<template>
  <el-dialog center title="充值" :visible.sync="innerVisible" width="700px">
    <el-form :model="model" label-position="left" :rules="rules" ref="ruleForm" label-width="100px">
      <el-row>
        <el-col :span="14" :offset="5">
          <el-form-item label="充值金额：" prop="inputMoney">
            <el-input v-model="model.inputMoney" :placeholder="'余额：' + totalMoney + '元'" style="position:relative;"></el-input>
          </el-form-item>
        </el-col>
        <!--<el-col :span="10"></el-col>-->
        <el-col class="text-center" :span="24" style="margin-top:10px;">
          <el-row>
            <el-col class="text-center" :span="5" :offset="6">
              <el-button @click="innerVisible = false" style="width:100%">取消</el-button>
            </el-col>
            <el-col class="text-center" :span="5" :offset="2">
              <el-button type="primary" :loading="btnLoading" @click="confirmMoney()" style="width:100%">确定</el-button>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import {isFormatNumber, range} from '@/utils/validate'
export default {
  props: {
    totalMoney: {
      type: Number,
      required: true
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    visible(val, oldVal) {
      if (val === oldVal) {
        return
      }
      if (val) {
        // 重置表单
        this.model.inputMoney = ''
        // this.resetFields()
        // 获取数据
      }
      this.innerVisible = val
    },
    // 如果内部有新值变化，更新外部visible
    innerVisible(val, oldVal) {
      if (val === oldVal) {
        return
      }
      this.$emit('update:visible', val)
    }
  },
  data() {
    return {
      innerVisible: this.visible,
      btnLoading: false,
      model: {
        inputMoney: '' // 金额
      },
      // 密码验证规则
      rules: {
        inputMoney: [
          { required: true, message: '请输入充值金额', trigger: 'change' },
          { validator: isFormatNumber, int: 8, decimal: 2, message: '请输入8位以下整数或2位小数', trigger: 'blur' },
          { validator: range, min: 0.01, message: '请输入大于0.01的正数', trigger: 'blur' }],
        paymentType: [
          {required: true, message: '充值方式必须', trigger: 'change'}
        ]}
    }
  },
  methods: {
    confirmMoney() {
      this.$refs.ruleForm.validate().then((result) => {
        if (!result) {
          return false
        } else {
          this.innerVisible = false
          this.$router.push({path: '/ss-fhb/myWallet/recharge/' + this.model.inputMoney + '/?totalMoney=' + this.totalMoney})
          console.log('OK11')
        }
      })
    }
  }
}
</script>
